import app
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import users
from models import PortfolioCategory, PortfolioItem

class PortfolioPage(webapp.RequestHandler):
    def get(self):
        items = PortfolioItem.all()
        cats = PortfolioCategory.all()
        self.response.out.write(template.render('templates/portfolio.html', {'items': items, 'cats': cats}))
        

class ImageHandler(webapp.RequestHandler):
    def get(self, id = None, type = 'full'):
        item = PortfolioItem.get_by_id(int(id)) or self.error(404)
        self.response.headers['Content-Type'] = 'image/jpeg'
        if type == 'thumb':
            self.response.out.write(item.thumbnail_image)
        elif type == 'orig':
            if users.IsCurrentUserAdmin(): # only admin can see it
                self.response.out.write(item.original_image)
        else:
            self.response.out.write(item.full_size_image)


def main():
    app.start(
        ('/portfolio', PortfolioPage),
        ('/portfolio/image/(\d+)/(\w+)', ImageHandler)
    )

if __name__ == "__main__":
    main()